home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d20 / msgq160s.arc / TEXTFILE.C < prev    next >
C/C++ Source or Header  |  1991-10-26  |  6KB  |  321 lines

  1. /*
  2.  * TEXTFILE.C - Write text files
  3.  *
  4.  * Msged/Q message editor for QuickBBS  Copyright 1990 by P.J. Muller
  5.  *
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <io.h>
  12. #include <ctype.h>
  13. #include <fcntl.h>
  14. #include <sys/stat.h>
  15.  
  16. #ifdef __MSC__
  17. #include <sys/types.h>
  18. #endif
  19.  
  20. #include "msged.h"
  21. #include "screen.h"
  22. #include "qmsgbase.h"
  23.  
  24. #include <assert.h>
  25.  
  26. #define TEXTLEN 200
  27.  
  28. static FILE *repapp(char *path);
  29.  
  30. void    import(LINE * l)
  31. {
  32.         char    line[TEXTLEN];
  33.     FILE   *fp;
  34.     static char fn[PATHLEN];
  35.     LINE   *n;
  36.     int key;
  37.  
  38.         memset(line, 0, sizeof(line));
  39.  
  40.     gotoxy(9, 1);
  41.     clreol();
  42.     set_color(co_info);
  43.     bputs("File to import? ");
  44.     key = bgets(fn, PATHLEN);
  45.     set_color(co_normal);
  46.     if (key == ABORT) return;
  47.  
  48.     if ((fp = fopen(fn, "rt")) != NULL) {
  49.                 while (fgets(line, min(maxx,TEXTLEN), fp) != NULL) {
  50.             if (l->text != NULL) {
  51.                 if ((n = (LINE *) calloc(1, sizeof(LINE))) == NULL)
  52.                   goto error;
  53.  
  54.                 n->next = l->next;
  55.                 if (n->next == NULL)
  56.                     msgbuf.last = n;
  57.                 else
  58.                     n->next->prev = n;
  59.                 n->prev = l;
  60.                 l->next = n;
  61.                 l = n;
  62.             }
  63.             else
  64.                 n = l;
  65.             if ((n->text = strdup(line)) == NULL)
  66.               goto error;
  67.                         memset(line, 0, sizeof(line));
  68.         }
  69.         fclose(fp);
  70.     }
  71.  
  72.     return;
  73.  
  74. error:        /* out of memory error */
  75.     gotoxy(9, 1);
  76.     clreol();
  77.     bputs("Not enough memory, press any key");
  78.     video_update();
  79.     (void)getkey();
  80.     return;
  81. } /* import */
  82.  
  83. void    export(LINE * f)
  84. {
  85.     FILE   *fp;
  86.     static char fn[PATHLEN];
  87.     int key;
  88.  
  89.     gotoxy(9, 1);
  90.         clreol();
  91.     set_color(co_info);
  92.     bputs("File name to write to? ");
  93.     if (fn[0] == EOS)
  94.       strcpy(fn, outfile);
  95.     key = bgets(fn, sizeof(fn));
  96.     set_color(co_normal);
  97.     if (key == ABORT) return;
  98.  
  99.     if ((fp = repapp(fn)) != NULL) {
  100.  
  101.         fputc('\n', fp);
  102.  
  103.         for (; f != NULL; f = f->next)
  104.             if ((f->text != NULL) && ((*(f->text) != '\01') || shownotes)) {
  105.                 fputs(f->text, fp);
  106.                 if (strchr(f->text, '\n') == NULL)
  107.                     fputc('\n', fp);
  108.             }
  109.  
  110.         fclose(fp);
  111.     }
  112.     else {
  113.       gotoxy(9, 1);
  114.       clreol();
  115.       set_color(co_warn);
  116.       bputs("Can't open file, press any key :");
  117.       video_update();
  118.       (void)getkey();
  119.       set_color(co_normal);
  120.         }
  121.  
  122.     showheader(message);
  123. }
  124.  
  125. void    writetxt()
  126. {
  127.     LINE   *f = msgbuf.first;
  128.     FILE   *fp;
  129.     static char fn[PATHLEN];
  130.     int key;
  131.  
  132.     gotoxy(9, 1);
  133.     clreol();
  134.     set_color(co_info);
  135.     bputs("File name to write to? ");
  136.     if (fn[0] == EOS)
  137.       strcpy(fn, outfile);
  138.     key = bgets(fn, sizeof(fn));
  139.     set_color(co_normal);
  140.     if (key == ABORT) return;
  141.  
  142.     if ((fp = repapp(fn)) == NULL) {
  143.       gotoxy(9, 1);
  144.       clreol();
  145.       set_color(co_warn);
  146.       bputs("Can't open file, press any key :");
  147.       video_update();
  148.       (void)getkey();
  149.       set_color(co_normal);
  150.       return;
  151.     }
  152.  
  153.     fprintf(fp, "\n%03d/%03d %s\n",
  154.         boardmsg(CurBoard, message.header.msgnum), countmsg(CurBoard),
  155.         timedate(message.header.posttime, message.header.postdate, NO));
  156.  
  157.     fprintf(fp, "From:   %s", message.header.from);
  158.  
  159.     if (arealist[area].netmail)
  160.         fprintf(fp, " of %s", formaddr(message.from,Z_O|N_A|P_O|D_O|A_INT));
  161.     /*fprintf(fp, " of %s", formaddr(thisnode[CurAKA],Z_O|N_A|P_O|D_O|A_INT));*/
  162.  
  163.     fputc('\n', fp);
  164.  
  165.     fprintf(fp, "To:     %s", message.header.to);
  166.  
  167.     if (arealist[area].netmail) {
  168.         fprintf(fp, " of %s", formaddr(message.to,Z_O|N_A|P_O|D_O|A_INT));
  169.  
  170.     }
  171.  
  172.     fputc('\n', fp);
  173.  
  174.     if (message.header.bits.is_file)
  175.         fprintf(fp, "Files:  %s", message.header.subj);
  176.     else
  177.         fprintf(fp, "Subj:   %s", message.header.subj);
  178.  
  179.     fputc('\n', fp);
  180.  
  181.     fputs("Attr:   ", fp);
  182.     fputs(bits2ascii(message.header.bits,TRUE), fp);
  183.     fputc('\n', fp);
  184.  
  185.     {
  186.       char buf[80];
  187.       strcpy(buf,"------------------------------------------------\n");
  188.       memcpy(buf,arealist[area].description,
  189.         min(strlen(arealist[area].description),strlen(buf)-1));
  190.       fputs(buf, fp);
  191.     }
  192.  
  193.     for (; f != NULL; f = f->next)
  194.         if ((f->text != NULL) && ((*(f->text) != '\01') || shownotes)) {
  195.             fputs(f->text, fp);
  196.             if (strchr(f->text, '\n') == NULL)
  197.                 fputc('\n', fp);
  198.         }
  199.  
  200.     if (isatty(fileno(fp)))
  201.         fputc(12, fp);
  202.  
  203.     fclose(fp);
  204. } /* writetxt */
  205.  
  206. static FILE *repapp(char *path)
  207. {
  208.     FILE *fp;
  209.     int ch;
  210.  
  211.  
  212.     if ((fp = fopen(path,"r")) == NULL)
  213.       return (fp = fopen(path,"wt"));
  214.  
  215.     if (isatty(fileno(fp))) {
  216.         fclose(fp);
  217.         return(fp = fopen(path,"wt"));
  218.     }
  219.     fclose(fp);
  220.  
  221.     gotoxy(9,1);
  222.     clreol();
  223.     set_color(co_hilite);
  224.     bputc('r');
  225.     set_color(co_info);
  226.     bputs("eplace or ");
  227.     set_color(co_hilite);
  228.     bputc('a');
  229.     set_color(co_info);
  230.         bputs("ppend? ");
  231.         video_update();
  232.  
  233.     ch = getkey() & 0x7f;
  234.     ch = tolower(ch);
  235.     if (ch == 0x1b)
  236.         return(NULL);
  237.  
  238.     while ((ch != 'a') && (ch != 'r')) {
  239.         ch = 0x7f & getkey();
  240.         ch = tolower(ch);
  241.         if (ch == 0x1b)
  242.             return(NULL);
  243.     }
  244.  
  245.     if (ch == 'a')
  246.         fp = fopen(path,"at");
  247.     else
  248.         fp = fopen(path,"wt");
  249.  
  250.     return(fp);
  251. }
  252.  
  253. /*
  254.  * Write the whole text buffer to a file, overwrite the path if it exists
  255.  */
  256.  
  257. #ifdef EXTERNEDIT
  258. BOOLEAN writebuffile(char *path, BUFFER *buf)
  259. {
  260.   LINE *p;
  261.   FILE *f;
  262.   BOOLEAN ok = FALSE;
  263.  
  264.   if ((f = fopen(path,"wt")) == NULL)
  265.     return FALSE;
  266.  
  267.   p = buf->first;
  268.   while (p != NULL) {
  269.     if ((p->text != NULL) && (shownotes || (*(p->text) != '\01'))) {
  270.       if (strcmp(p->text,"---") == 0) break;    /* stop at tearline */
  271.       if (fputs(p->text, f) == EOF) break;
  272.       if (strchr(p->text, '\n') == NULL)    /* hard CR */
  273.     fputc('\n', f);
  274.     }
  275.     p = p->next;
  276.   }
  277.   ok = (p == NULL);
  278.  
  279.   if (fclose(f) != 0)
  280.     ok = FALSE;
  281.  
  282.   return ok;
  283. } /* writebuffile */
  284.  
  285. BOOLEAN readbuffile(char *path, BUFFER *buf)
  286. {
  287.   struct stat st;
  288.   int fd = -1;
  289.   char *text = NULL;
  290.   BOOLEAN ok = FALSE;
  291.  
  292.   if ((fd = open(path, O_RDONLY|O_BINARY)) == -1)
  293.     goto fail;
  294.   if (fstat(fd, &st) == -1)
  295.     goto fail;
  296.  
  297.   clearbuffer(buf);
  298.  
  299.   if (st.st_size != 0) {
  300.     if ((text = malloc(st.st_size)) == NULL)
  301.       goto fail;
  302.     if (read(fd, text, st.st_size) != st.st_size)
  303.       goto fail;
  304.   } else {
  305.     text = strdup("\r");
  306.   }
  307.   *buf = buffer(&text);
  308.  
  309.   ok = TRUE;
  310. fail:
  311.   if (fd != -1)
  312.     if (close(fd) != 0)
  313.       ok = FALSE;
  314.  
  315.   if (text != NULL)
  316.     free(text);
  317.  
  318.   return ok;
  319. } /* readbuffile */
  320. #endif
  321.